/**   
 * Copyright © 2019 eSunny Info. Tech Ltd. All rights reserved.
 * 
 * @Description:TODO(功能)
 * @className OrderDetailsDaoImpl.java
 * @Package: com.erp.dao 
 * @author: yj   
 * @date: 2019年2月22日 下午1:33:22 
 */
package com.erp.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.erp.po.OrderDetail;
import com.erp.po.Orders;

/**
 * @className OrderDetailsDaoImpl.java
 * @author yj
 * @Description:TODO(功能)
 * @Date 2019年2月22日
 */
public class OrderDetailsDaoImpl extends BaseDao implements OrderDetailsDao {

	/* 
	 * 批量增加订单详情
	 */
	@Override
	public int[] insertMoveOrderDetails(Connection conn, OrderDetail... orderDetail) throws SQLException {
		String sql = "insert into order_detail(id, orderid, productid, quantity, discount) values(order_detail_seq.nextval,?,?,?,?)";
		Object[][] obj = new Object[orderDetail.length][];
		for (int i = 0; i < orderDetail.length; i++) {
			obj[i] = new Object[] {
					orderDetail[i].getOrder().getOrderID(),
					orderDetail[i].getProduct().getProductId(),
					orderDetail[i].getQuantity(),
					orderDetail[i].getDiscount()};
		}
		return super.insertMoveData(conn, sql, obj);
	}

	/* 
	 * 新建订单
	 */
	@Override
	public int insertOrderByDetail(Connection conn, Orders order) throws SQLException {
		String sql = "insert into ORDERS(ORDERID, ORDER_DATE, customerid, empid) values(?,?,?,?)";
		Object[] obj = {order.getOrderID(), order.getOrderDate(), 
				order.getCustomer().getCustomerID(),order.getEmployees().getEmpID()};
		return super.updateData(conn, sql, obj);
	}

	/* 
	 * 根据orderID 查询 对应的订单详情
	 */
	@Override
	public ResultSet selectOrderDetailByOrderID(Connection conn, String orderID) throws SQLException {
		String sql = "select \r\n" + 
				"  det.id,\r\n" + 
				"  det.quantity,\r\n" + 
				"  det.discount, \r\n" + 
				"  ord.orderid, \r\n" + 
				"  to_char(ord.order_date,'yyyy-MM-dd') order_date,\r\n" + 
				"  cust.customerid,\r\n" + 
				"  cust.customer_name,\r\n" + 
				"  emp.empid,\r\n" + 
				"  emp.emp_name,\r\n" + 
				"  prod.productid,\r\n" + 
				"  prod.product_name\r\n" + 
				"from \r\n" + 
				"  orders ord, \r\n" + 
				"  order_detail det,\r\n" + 
				"  customers cust,\r\n" + 
				"  employees emp,\r\n" + 
				"  products prod\r\n" + 
				"where \r\n" + 
				"  prod.productid = det.productid and\r\n" + 
				"  ord.orderid = det.orderid and\r\n" + 
				"  ord.customerid = cust.customerid and\r\n" + 
				"  ord.empid = emp.empid and\r\n" + 
				"  ord.orderid = ? ";
		return super.selectData(conn, sql, orderID);
	}

	/* 
	 * 修改订单-详情
	 */
	@Override
	public int[] updateOrderDetails(Connection conn, OrderDetail... orderDetail) throws SQLException {
		String sql ="update order_detail set \r\n" + 
				"       productid = ? ,\r\n" + 
				"       quantity = ? ,\r\n" + 
				"       discount = ? \r\n" + 
				" where \r\n" + 
				"       orderid = ?"
				+ " and id = ? ";
		Object[][] detail = new Object[orderDetail.length][];
		for (int i = 0; i < orderDetail.length; i++) {
			
			System.out.println(orderDetail[i].getProduct().getProductId());
			detail[i] = new Object[] {
					orderDetail[i].getProduct().getProductId(),
					orderDetail[i].getQuantity(),
					orderDetail[i].getDiscount(),
					orderDetail[i].getOrder().getOrderID(),
					orderDetail[i].getId()
			};
		}
		return super.insertMoveData(conn, sql, detail);
	}

	/* 
	 * 根据orderID 查询 对应的订单详情的数量
	 */
	@Override
	public ResultSet selectDetailCountByOrderID(Connection conn, String orderID) throws SQLException {
		String sql = "select count(*) from ORDER_DETAIL where orderid = ? ";
		return super.selectData(conn, sql, orderID);
	}

	/* 
	 * 新建订单-详情 根据订单id
	 */
	@Override
	public int[] insertDetailByOrderId(Connection conn , OrderDetail... orderDetail) throws SQLException {
		String sql = "insert into order_detail(id, orderid, productid, quantity, discount) "
				+ " values(order_detail_seq.nextval,?,?,?,?) ";
		Object[][] obj = new Object[orderDetail.length][];
		for (int i = 0; i < orderDetail.length; i++) {
			obj[i] = new Object[] {
					orderDetail[i].getOrder().getOrderID(),
					orderDetail[i].getProduct().getProductId(),
					orderDetail[i].getQuantity(),
					orderDetail[i].getDiscount(),
					};
		}
		return super.insertMoveData(conn, sql, obj);
	}

	/* 
	 * 根据id 查询 对应的订单详情
	 */
	@Override
	public ResultSet selectDetailByIds(Connection conn, String orderId , int detailId , int productId) throws SQLException {
		ArrayList list = new ArrayList();
		String sql = "select * from order_detail where orderId = ? and productid = ? ";
		list.add(orderId);
		list.add(productId);
		if(detailId != 0) {
			sql = sql.concat(" and id = ? ");
			list.add(detailId);
		}
		
		return super.selectData(conn, sql, list.toArray());
	}

	/* 
	 * 根据detailid 删除订单-详情
	 */
	@Override
	public int deleteDetailById(Connection conn, int detailId) throws SQLException {
		String sql = "DELETE FROM order_detail where id = ? ";
		return updateData(conn, sql, detailId);
	}

	/* 
	 * 根据orderID 删除订单-详情
	 */
	@Override
	public int deleteDetailByOrder(Connection conn, String orderId) throws SQLException {
		String sql = "DELETE FROM order_detail where orderid = ? ";
		return super.updateData(conn, sql, orderId);
	}

}
